﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Voodoo;
using Voodoo.Data;
using Vodoo.Utility;

namespace Northwind.Data
{
    public class NorthwindWebLocator: IContainer
    {
        private NorthwindEntities GetContext()
        {
            if (HttpContextHelper.Contains<NorthwindEntities>())
                return HttpContextHelper.Get<NorthwindEntities>();
            else
            {
                var ctx = new NorthwindEntities();
                ctx.ContextOptions.LazyLoadingEnabled = false;
                ctx.ContextOptions.ProxyCreationEnabled = false;

                HttpContextHelper.Add<NorthwindEntities>(ctx);
                return ctx;
            }
            


            
        }

        public IRepository<T> ResolveRepository<T>() where T : class, new()
        {
            return new EntityFrameworkRepository<T, NorthwindEntities>(GetContext());
        }

        public T Resolve<T>() where T : class, new()
        {
            throw new NotImplementedException();
        }


        public IUnitOfWork GetUnitOfWork()
        {
            var ctx = new NorthwindEntities();
            ctx.ContextOptions.LazyLoadingEnabled = false;
            ctx.ContextOptions.ProxyCreationEnabled = true;
            return ctx;
        }


        public IRepository<T> ResolveRepository<T>(IUnitOfWork unitOfWork) where T : class, new()
        {
            return new EntityFrameworkRepository<T, NorthwindEntities>(unitOfWork);
        }
    }
}
